//
//  EnlargeImageViewController.m
//  Home
//
//  Created by lollipop on 15/10/10.
//  Copyright © 2015年 jobs_A. All rights reserved.
//

#import "EnlargeImageViewController.h"
#import "THLabel.h"
#import "UIImageView+WebCache.h"
#define zWidth  self.view.frame.size.width
#define zHeight self.view.frame.size.height

@interface EnlargeImageViewController ()<UIScrollViewDelegate,UIGestureRecognizerDelegate>
{
    NSMutableArray *_imageArray;
    UIView *zView;
    UIScrollView *zScrollView;
    THLabel *zLabel;
}

@end

@implementation EnlargeImageViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self initWithImageArray:self.imageArray PageIndex:self.pageIndex];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)initWithImageArray:(NSArray *)imageArray PageIndex:(int)pageIndex{
    
    _imageArray = [NSMutableArray arrayWithArray:imageArray];
    zView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, zWidth, zHeight)];
    zView.backgroundColor = [UIColor blackColor];
    //    label
    zLabel = [[THLabel alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
    zLabel.center = CGPointMake(zWidth/2, zHeight*19/20);
    
    zLabel.text = [NSString stringWithFormat:@"%d/%ld",pageIndex + 1,imageArray.count];
    zLabel.textAlignment = NSTextAlignmentCenter;
    [zLabel setShadowColor:[UIColor blackColor]];
    zLabel.backgroundColor = [UIColor clearColor];
    zLabel.textColor = [UIColor whiteColor];
    

    //    初始化scrollView
    zScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, zWidth, zHeight)];
    

    
    zScrollView.contentSize = CGSizeMake(zWidth *_imageArray.count, zHeight);
    zScrollView.scrollEnabled = YES;
    zScrollView.bounces = YES;
    zScrollView.pagingEnabled = YES;
    zScrollView.showsHorizontalScrollIndicator = NO;
    zScrollView.showsVerticalScrollIndicator = NO;
    zScrollView.directionalLockEnabled = YES;
    zScrollView.delegate = self;
    zScrollView.contentOffset =  CGPointMake(zWidth * pageIndex, 0);
    
  
    for (int i = 0 ; i < _imageArray.count; i ++ ) {
        
        UIImageView *zImageview = [[UIImageView alloc]initWithFrame:CGRectMake(zWidth * i, 0, zWidth, zHeight)];
//        zImageview.image = _imageArray[i];
        
        [zImageview sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",_imageArray[i]]] placeholderImage:[UIImage imageNamed:@"backImage"]];
        zImageview.contentMode = UIViewContentModeScaleAspectFit;
        
//        //添加缩放手势
//        
//        UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchView:)];
//        pinch.delegate = self;
//        zImageview.userInteractionEnabled = YES;
//        zImageview.multipleTouchEnabled = YES;
//        [zImageview addGestureRecognizer:pinch];
        
        //点击手势
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc ]init];
        tap.enabled = YES;
        tap.numberOfTapsRequired = 1 ;
        tap.numberOfTouchesRequired = 1;
        [tap addTarget:self action:@selector(cancleTapped:)];
        zImageview.userInteractionEnabled = YES;
        
        zImageview.tag = i;
        [zImageview addGestureRecognizer:tap];
        
        [zScrollView addGestureRecognizer:tap];
        [zScrollView addSubview:zImageview];
    }
    
    
    [zView addSubview:zScrollView];
    [zView addSubview:zLabel];
    [self.view addSubview:zView];
    
}


- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    int page = scrollView.contentOffset.x / zWidth;
    
    zLabel.text = [NSString stringWithFormat:@"%d/%ld",page + 1,_imageArray.count];
    
}

- (void)cancleTapped:(UITapGestureRecognizer *)gesture{
    
//    [self dismissViewControllerAnimated:YES completion:nil];
    
    [self.view removeFromSuperview];
    [self removeFromParentViewController];

    
}

#pragma mark - 缩放手势(捏合手势)有BUG暂时取消

- (void)pinchView:(UIPinchGestureRecognizer *)pinch
{
    UIImageView *imageView = (UIImageView *)pinch.view;
    
    pinch.view.transform = CGAffineTransformScale(pinch.view.transform, pinch.scale, pinch.scale);
    pinch.scale = 1;// 这个真的很重要!!!!!
    
    
//    CGFloat scale = pinch.scale;
//    
//    // 如果捏合手势刚刚开始
//    
//    if (pinch.state == UIGestureRecognizerStateBegan)
//        
//    {
//        
//        // 计算当前缩放比
//        
//        currentScale = imageView.image.size.width / srcImage.size.width;
//        
//    }
//    
//    // 根据手势处理器的缩放比例计算图片缩放后的目标大小
//    
//    CGSize targetSize = CGSizeMake(srcImage.size.width * scale * currentScale,
//                                   
//                                   srcImage.size.height * scale * currentScale);
//    
//    // 对图片进行缩放
//    
//    imageView.image = [srcImage imageByScalingToSize:targetSize];
    
}



@end
